<?php
if(isset($javascript)):
// load script in <head> section
$javascript->link('scriptaculous/lib/prototype', false);
endif;
?>
<script>
function selectusers(group){
    groupid=group.id.gsub('<?php echo $this->model ?>Group','');
    var url='<?php echo $html->url(array('controller'=>'ajaxes','action'=>'groupusers'))?>/'+groupid;
    var dajax=new Ajax.Request(url,{onSuccess:function doit(response){doselectusers(response,group)}});
}

function doselectusers(response,group){
    str=response.responseText.split('<');
    users=str[0].split(',');
    //users=response.responseText.split(',');
    for(var index=0,len=users.length; index<len; ++index){
        var user=users[index];
        if($('UserUser'+user)){
            $('UserUser'+user).checked=group.checked;
        }
    }
}

function unselectgroup(user){
    userid=user.id.gsub('UserUser','');
    var url='<?php echo $html->url(array('controller'=>'ajaxes','action'=>'usergroups'))?>/'+userid;
    var dajax=new Ajax.Request(url,{onSuccess:dounselectgroups});
}

function dounselectgroups(response){
    str=response.responseText.split('<');
    groups=str[0].split(',');
    //groups=response.responseText.split(',');
    for(var index=0,len=groups.length; index<len; ++index){
        var group=groups[index];
        if($('<?php echo $this->model ?>Group'+group)){
            $('<?php echo $this->model ?>Group'+group).checked=false;
        }
    }
}

function checkAll(all,user){
    a=user.split(',');
    for(i=0;i<a.length;i++){
        $('UserUser'+a[i]).checked=all.checked;
    }
}

function checkAllGroup(all,group){
    a=group.split(',');
    for(i=0;i<a.length;i++){
        $('MeetingGroup'+a[i]).checked=all.checked;
        selectusers($('MeetingGroup'+a[i]));
    }
}
</script>
<table cellpadding='0' cellspacing='0' width='100%'><tr>
<?php
if($from == 'meetings/add'){
    if(count($groups)){
        $group_ids=implode(',',array_keys($groups));
        echo "<td width='40%'>";
        echo $form->label(__('Select by group',true));
        $selectAll=$html->div('input checkbox',
            $form->checkbox('Select All',array('checked'=>false,'onClick'=>'checkAllGroup(this,"'.$group_ids.'")')).$form->label(__('Select All Group',true))
        );
        echo $html->div('radio',$selectAll.$form->input('group',array('label'=>false,'options'=>$groups,'multiple'=>'checkbox','onChange'=>'selectusers(this)')));
        echo "</td>";
    }
    $output = "<td>";
    $output.= $form->label('User.User',__("Members",true));
    $i = 0;
    if (count($users)) {
        $user_ids=implode(',',Set::extract($users,'{n}.User.id'));
        $output.= $html->div('input checkbox',
            $form->checkbox('Select All',array('checked'=>false,'onClick'=>'checkAll(this,"'.$user_ids.'")')).$form->label(__('Select All',true))
        );
        foreach ($users as $key=>$user) {
            switch ($user['Role']['name']) {
                case 'pengerusi' : 
                case 'chairperson' : $rolevalue = '2'; break;
                case 'admin' : 
                case 'urusetia' : $rolevalue = '1'; break;
                //case 'ahli':
                case 'member' : $rolevalue = '3'; break;
                default : $rolevalue ='3';
            }
            $output.= "<div>";
            $output.= $html->div('input select',
                        $form->select('User.AttendAs.'.$user['User']['id'], array (array('3'=>__('Member',true),'4'=>__('Member (external)',true),'2'=>__('Chairperson',true),'1'=>__('Secretariat',true))), $rolevalue, '', false )
                        );
            $output.= $html->div('input checkbox',
                        $form->checkbox('User.User.'.$i++,array('value'=>$user['User']['id'],'id'=>'UserUser'.$user['User']['id'],'checked'=>false,'onClick'=>'unselectgroup(this)')).$form->label('User.User',$user['User']['name'])
                        );
            $output.= "</div>";
        }
        echo $html->div('radio',$output);
    } else {
        echo $html->div('radio',__('No members have been assigned under the committee',true));
    }
} elseif($from == 'meetings/edit') {
    if(count($groups))
        $group_ids=implode(',',array_keys($groups));
        echo "<td width='40%'>";
        echo $form->label(__('Select by group',true));
        $selectAll=$html->div('input checkbox',
            $form->checkbox('Select All',array('checked'=>false,'onClick'=>'checkAllGroup(this,"'.$group_ids.'")')).$form->label(__('Select All Group',true))
        );
        echo $html->div('radio',$selectAll.$form->input('group',array('label'=>false,'options'=>$groups,'multiple'=>'checkbox','onChange'=>'selectusers(this)')));
        echo "</td>";
   
    $temp = array();$attendAs = array();
    foreach ($users2 as $att) {
        if ($att['Attendance']['deleted']==0  && $att['Attendance']['user_id']){
            array_push($temp, $att['Attendance']['user_id']) ;
            $attendAs[$att['Attendance']['user_id']] = $att['Attendance']['attend_as'] ; 
        }
    } 
    $output = "<td>";
    $output.= $form->label('User.User',__("Members",true));
    $user_ids=implode(',',array_keys($users));
    $output.= $html->div('input checkbox',
            $form->checkbox('Select All',array('checked'=>false,'onClick'=>'checkAll(this,"'.$user_ids.'")')).$form->label(__('Select All',true))
        );
    $i = 0;
    foreach ($users as $key=>$user) {
        in_array($key, $temp) ? $checked = 'checked': $checked = ''; // if invited previouly, check the box) {
        (in_array($key, $temp) && isset($attendAs[$key])) ? '' : $attendAs[$key] = '3';
        $output.= "<div>";
        $output.= $html->div('input select',
                    $form->select('User.AttendAs.'.$key, array (array('3'=>__('Member',true),'4'=>__('Member (external)',true),'2'=>__('Chairperson',true),'1'=>__('Secretariat',true))), $attendAs[$key], '', false )
                    );

        $output.= $html->div('input checkbox',
                    $form->checkbox('User.User.'.$i++,array('value'=>$key,'id'=>'UserUser'.$key,'checked'=>$checked,'onClick'=>'unselectgroup(this)')).$form->label('User.User',$user)
                    );
        $output.= "</div>";
    }
    $output.="</td>";
    echo $html->div('radio',$output);
}else{ 
    if(count($groups)){
        echo "<td width='40%'>".$html->div('radio',$form->input('group',array('label'=> __('Select by group', true),'options'=>$groups,'multiple'=>'checkbox','onChange'=>'selectusers(this)')))."</td>";
    }
    //echo "<td width='50%'>".$html->div('radio',$form->input('User.User',array('options'=>$users,'onClick'=>'unselectgroup(this)','multiple'=>'checkbox')))."</td>";
    $output = "<td>";
    $output.= $form->label('User.User',__("Members",true));
    $i = 0;
    if (count($users)) {
        foreach ($users as $key=>$user) {
            $output.= "<div>";
            $output.= $html->div('input select',
                        $form->select('User.AttendAs.'.$key, array (array('3'=>__('Member',true),'4'=>__('Member (external)',true),'2'=>__('Chairperson',true),'1'=>__('Secretariat',true))), '3', '', false )
                        );
            $output.= $html->div('input checkbox',
                        $form->checkbox('User.User.'.$i++,array('value'=>$key,'id'=>'UserUser'.$key,'checked'=>false,'onClick'=>'unselectgroup(this)')).$form->label('User.User',$user)
                        );
            $output.= "</div>";
        }
        echo $html->div('radio',$output);
    } else {
        echo $html->div('radio',__('No members have been assigned under the committee',true));
    }
}
?>
</tr></table>
